<template>
    <div class="bagc">
        <div class="wenzi">
            <div><i class="el-icon-bicycle"></i>共享单车管理系统</div>
        </div>
        <div class="center">
            <img src="../assets/共享单车1.webp">
            <div class="denglu">
                <el-form :model="editData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                    <el-form-item label="账号" prop="username">
                        <el-input v-model="editData.username" placeholder="请输入账号"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input type="password" v-model="editData.password" placeholder="请输入密码"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <div style="display: flex;justify-content: center;align-items: center; ">
                            <el-button type="primary" @click="login('ruleForm')" style="width: 100%;">登录
                            </el-button>
                        </div>
                    </el-form-item>
                </el-form>
                <div class="zhuce" @click="register">
                    <p style="font-size: 14px; margin-left: 15px;">注册账号</p>
                </div>
            </div>
        </div>
        <footer class="banquan" style="height: 2%;">Xubuling © 版权所有 MrXu 2022</footer>
    </div>
</template>
<script>
    import { userLoginAPI, updateonlineAPI, myInformationAPI } from "@/api";
    import { mapMutations } from 'vuex'
    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    callback();
                }
            };
            return {
                rules: {
                    username: [
                        { required: true, message: '请输入账号', trigger: 'blur' },
                        { min: 3, max: 10, message: '账号长度在 3 到 10 个字符', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, validator: validatePass, trigger: 'blur' },
                        { min: 6, max: 12, message: '密码长度在 6 到 12 个字符', trigger: 'blur' }
                    ],
                },
                editData: {
                    username: '',
                    password: '',
                    online: 1
                },
                onlineData: {
                    id: '',
                    online: 1
                },
                checked: false,
                onlineTimer: ''
            }
        },
        created() {

        },
        beforeDestroy() {
            clearInterval(this.onlineTimer);

        },
        methods: {
            async init() {
                const { data: res } = await myInformationAPI()
                console.log(res)
                if (res.status == 0) {
                    this.onlineData.id = res.data.id
                    this.updateonline()
                }
            },
            async updateonline() {
                const { data: res } = await updateonlineAPI(this.onlineData)
                console.log(res)
            },
            ...mapMutations(['updateToken']),
            login(formName) {
                this.$refs[formName].validate(async valid => {
                    if (valid) {
                        if (this.editData.username == '') {
                            this.$message.error('请输入账号');
                        } else if (this.editData.password == '') {
                            this.$message.error('请输入密码');
                        }
                        else {
                            const { data: res } = await userLoginAPI(this.editData)
                            if (res.status == 0) {
                                if (res.type === 1) {
                                    this.$message({
                                        showClose: true,
                                        message: '登录成功',
                                        type: 'success'
                                    });
                                    this.updateToken(res.token)
                                    this.$router.push({ path: '/home' })
                                } else {
                                    this.updateToken(res.token)
                                    this.$message({
                                        showClose: true,
                                        message: '登录成功',
                                        type: 'success'
                                    });
                                    this.onlineTimer = setTimeout(() => {
                                        this.init()
                                        this.$router.push({ path: '/user' })
                                    }, 1000);
                                }
                            } else {
                                this.$message.error(res.message)
                            }
                        }
                    } else {
                        return false;
                    }
                });

            },
            register() {
                this.$router.push({ path: '/register' })
            }
        },
    }
</script>
<style scoped>
    body {
        width: 100%;
        height: 100%;
    }

    html {
        width: 100%;
        height: 100%;
    }

    .bagc {
        position: absolute;
        width: 100%;
        height: 100%;
        background: url(../assets/共享单车.webp);
        /* background-size: 100% 100%;  */
    }

    i {
        height: 20px;
        width: 30px;
    }

    .wenzi {
        margin: 20px 0 0 20px;
    }

    .wenzi>div {
        font-size: 30px;
        font-family: "楷体";
    }

    .wenzi>div>i {
        height: 10%;
    }

    .center {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 85%;
    }

    .center>img {
        width: 23.5%;
        height: 51.5%;
        display: inline-block;
        float: left;
    }

    .denglu {
        padding: 4% 3.5% 1% 0;
        background-color: aliceblue;
        width: 20%;
        height: 39%;
        display: inline-block;
        float: right;
    }

    .zhuce {
        color: #818181;
    }

    .zhuce:hover {
        color: blue;
        cursor: pointer;
    }

    .banquan {
        color: black;
        font-size: 13px;
        text-align: center;
        height: 60px;
    }
</style>